package com.alien.admin.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alien.admin.exception.BadRequestException;
import org.hibernate.validator.internal.constraintvalidators.hv.EmailValidator;


/**
 * 校验工具类
 *
 * @author: alien.guo
 * @date: 2020/12/24
 */
public class ValidationUtil{

    /**
     * 验证空
     */
    public static void isNull(Object obj, String entity, String parameter , Object value) {
        if (ObjectUtil.isNull(obj)) {
            String msg = entity + " 不存在: "+ parameter +" is "+ value;
            throw new BadRequestException(msg);
        }
    }

    /**
     * 验证空
     */
    public static void isEmpty(String name, Object value) {
        if (ObjectUtil.isNull(value)) {
            String msg = name + "不能为空";
            throw new BadRequestException(msg);
        }
    }

    /**
     * 验证相等
     */
    public static void isEqual(Object src, Object target, String msg) {
        if (src == target) {
            throw new BadRequestException(msg);
        }
    }

    /**
     * 验证是否为邮箱
     */
    public static boolean isEmail(String email) {
        return new EmailValidator().isValid(email, null);
    }
}
